/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package linecreator;

import java.awt.AWTException;
import java.awt.BasicStroke;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.Robot;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JLabel;

/**
 *
 * @author Marko
 */
public class Platno extends Canvas {

    ArrayList<Point> points;
    boolean prati = false;
    JLabel status;
    Image img;

    LineCreatorView lv;

    public void setPrati(boolean prati) {
        this.prati = prati;
    }

    public Image getImg() {
        return img;
    }

    public Platno(int width, int height, Point p, JLabel stat, ArrayList<Point> alp, Image i, LineCreatorView lv) {
        super();
        status = stat;
        postaviPlatno(width, height, p);
        if (null == alp) {
            points = new ArrayList<Point>();
        } else {
            points = alp;
        }

        img = i;
        this.lv=lv;

    }

    public void postaviPlatno(int width, int height, Point p) {
        setSize(width, height);
        setLocation(p);
        setBackground(Color.lightGray);
        addMouseListener(new java.awt.event.MouseAdapter() {

            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                if (prati == true && evt.getButton() == java.awt.event.MouseEvent.BUTTON1) {
                    Point p = evt.getPoint();
                    points.add(p);
                    lv.getTextField(3).setText("" + (int)p.getX());
                    lv.getTextField(4).setText("" + (int)p.getY());
                    status.setText("dodata je " + points.size() + ". tacka");
                    repaint();
                    //update(evt.getComponent().getGraphics());
                } else {
                    prati = false;
                    com.sun.awt.AWTUtilities.setWindowOpacity(LineCreatorApp.getApplication().getMainFrame(), 1);
                   lv.getButton(2).setEnabled(true);
                   lv.getButton(3).setEnabled(true);
                   lv.addLine();
                }
            }
        });

        addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {

            @Override
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                if(prati)
                {
                    try {
                        Robot r = new Robot();
                        Color centar = r.getPixelColor(evt.getXOnScreen(), evt.getYOnScreen());
                        Color levo = r.getPixelColor(evt.getXOnScreen() - 3, evt.getYOnScreen());
                        Color dole = r.getPixelColor(evt.getXOnScreen(), evt.getYOnScreen() + 3);
                        Color desno = r.getPixelColor(evt.getXOnScreen() + 3, evt.getYOnScreen());
                        Color gore = r.getPixelColor(evt.getXOnScreen(), evt.getYOnScreen() - 3);

                       // status.setText(centar.toString() + levo.toString() + dole.toString());
                       status.setText(evt.getPoint().toString());
                        if (colCmp(centar, Color.black)) {
                            if ((colCmp(centar, levo) && colCmp(centar, dole))
                                    || (colCmp(centar, levo) && colCmp(centar, gore))
                                    || (colCmp(centar, desno) && colCmp(centar, gore))
                                    || (colCmp(centar, desno) && colCmp(centar, dole))) {
                                Platno.this.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
                            } else {
                                Platno.this.setCursor(Cursor.getDefaultCursor());
                            }
                        } else {
                            Platno.this.setCursor(Cursor.getDefaultCursor());
                        }
//                        status.setBackground(r.getPixelColor(evt.getXOnScreen(), evt.getYOnScreen()));
//                        status.setForeground(r.getPixelColor(evt.getXOnScreen(), evt.getYOnScreen()));
//                        status.setText(evt.getLocationOnScreen().toString());
                    } catch (AWTException ex) {
                        Logger.getLogger(Platno.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        });
    }


    private boolean colCmp(Color c1, Color c2)
    {
        int treshold  = 20;
        return((treshold>Math.abs(c1.getRed()-c2.getRed())) && (treshold>Math.abs(c1.getGreen()-c2.getGreen())) && (treshold>Math.abs(c1.getBlue()-c2.getBlue())));
    }
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.drawImage(img, 0, 0, null);
        g.setColor(Color.red);


        int[] nizX = new int[points.size()];
        int[] nizY = new int[points.size()];
        int i = 0;
        for (Point p : points) {
            nizX[i] = (int) p.getX();
            nizY[i++] = (int) p.getY();
        }
        if (points.size() > 1) {
            //g.drawPolyline(nizX, nizY, points.size());
            Graphics2D g2d = (Graphics2D) g;
            g2d.setStroke(new BasicStroke(2F));
            g2d.drawPolyline(nizX, nizY, points.size());
        }
    }
    // public void paintComponent(Graphics g) {
    // g.drawImage(img, 0, 0, null);
    //}
}
